package com.op.parcel.manage.modules.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.op.parcel.boot.support.Condition;
import com.op.parcel.boot.support.Query;
import com.op.parcel.common.api.R;
import com.op.parcel.manage.modules.sys.entity.Permission;
import com.op.parcel.manage.modules.sys.service.IPermissionService;
import com.op.parcel.manage.modules.sys.vo.PermissionVO;
import com.op.parcel.manage.modules.sys.wrapper.PermissionWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collections;

/**
 * 控制器
 *
 * @author xuan
 * @since 2019-01-09
 */
@RestController
@AllArgsConstructor
@RequestMapping("permission")
@Api(value = "", tags = "接口")
public class PermissionController {

	private IPermissionService permissionService;

	/**
	 * 详情
	 */
	@GetMapping("detail")
	@ApiOperation(value = "详情", notes = "传入permission", position = 1)
	public R detail(Permission permission) {
		Permission detail = permissionService.getOne(Condition.getQueryWrapper(permission));
		PermissionWrapper permissionWrapper = new PermissionWrapper();
		return R.success(permissionWrapper.entityVO(detail));
	}

	/**
	 * 分页
	 */
	@GetMapping("list")
	@ApiOperation(value = "分页", notes = "传入permission", position = 2)
	public R list(Permission permission, Query query) {
		IPage<Permission> pages = permissionService.page(Condition.getPage(query), Condition.getQueryWrapper(permission));
		PermissionWrapper permissionWrapper = new PermissionWrapper();
		return R.success(permissionWrapper.pageVO(pages));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("saveOrUpdate")
	@ApiOperation(value = "新增或修改", notes = "传入permission", position = 3)
	public R saveOrUpdate(@Valid @RequestBody Permission permission) {
		return R.status(permissionService.saveOrUpdate(permission));
	}


	/**
	 * 删除
	 */
	@PostMapping("remove")
	@ApiOperation(value = "逻辑删除", notes = "传入ids", position = 4)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(permissionService.removeByIds(Collections.singleton(ids.split(","))));
	}


}
